package com.gexada.solr.dataimport.transformer;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TimeZone;


public class UTCDataTransformer {

	public Object transformRow(Map<String, Object> row) throws SQLException {
		
		TimeZone utc = TimeZone.getTimeZone("Asia/Shanghai");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'");
		sdf.setTimeZone(utc);
		
		for (Map.Entry<String, Object> item : row.entrySet()) {
			Object value = item.getValue();
			if (value != null && (oracle.sql.DATE.class.isAssignableFrom(value.getClass()) || oracle.sql.TIMESTAMP.class.isAssignableFrom(value.getClass()))) {
				Long time = null;
				if(value instanceof oracle.sql.TIMESTAMP) {
					oracle.sql.TIMESTAMP dat = (oracle.sql.TIMESTAMP)value;
					time = dat.timestampValue().getTime();
				} else if(value instanceof oracle.sql.DATE) {
					oracle.sql.DATE dat = (oracle.sql.DATE)value;
					time = dat.timestampValue().getTime();
				}
				String v = sdf.format(time);
				item.setValue(v);
			}
		}
		return row;
	}
}
